import fmeobjects
import numpy as np


class FeatureCreator(object):
    """Template Class Interface:
    When using this class, make sure its name is set as the value of the 'Class
    to Process Features' transformer parameter.
    """

    def __init__(self):
        """Base constructor for class members."""
        pass

    def input(self, feature):
        """This method is called once by FME to create FME Feature(s).
        A created Feature is emitted through the self.pyoutput() method.
        Any number of Features can be created and emitted by this method.

        :param fmeobjects.FMEFeature feature: Dummy FME Feature passed in by
            FME. It can be ignored.
        """
        newFeature = fmeobjects.FMEFeature()
        mesh = fmeobjects.FMEMesh()
        num_segments_on_arc = 10
        num_segments_per_section = 18
        pipe_height = 1
        ptO = np.asarray([-0.9, 5.2, 0.6])
        ptA = np.asarray([-2.47, 1.87, 0.5])
        ptB = np.asarray([3.1, 6.5, 0.6])
        ptC = np.asarray([-1.1, 5.2, 5])



        vertices_start, vertices_start2, vertices_end, vertices_end2, vertices_outside, vertices_center, vertices_arc_segments = calculate_3t(
            ptO, ptA,
            ptB, ptC,
            pipe_height,
            num_segments_on_arc,
            num_segments_per_section)
        vertices_arc_segments = np.concatenate(vertices_arc_segments)
        vertices = np.concatenate(
            [vertices_start, vertices_start2, vertices_end, vertices_end2, vertices_outside, vertices_center,
             vertices_arc_segments])

        for vertex in vertices:
            mesh.appendVertex(vertex[0], vertex[1], vertex[2])

        # 构建端点的面
        tmp_index_array_start = []
        tmp_index_array_end = []
        tmp_index_array_outside = []
        tmp_index_array_center = []
        tmp_index_array_se = []
        for k in range(num_segments_per_section):
            tmp_index_array_start.append(k)
            tmp_index_array_end.append(3 * num_segments_per_section - k - 1)
            tmp_index_array_outside.append(4 * num_segments_per_section + k)
            tmp_index_array_center.append(6 * num_segments_per_section  -k-1)
            #
            if k < num_segments_per_section - 1:
                mesh.addMeshPart(0, [k + 1, k,
                                     num_segments_per_section + k, num_segments_per_section + k + 1],
                                 None,
                                 None)
                mesh.addMeshPart(0, [2 * num_segments_per_section + k, 2 * num_segments_per_section + k + 1,
                                     3 * num_segments_per_section + k + 1, 3 * num_segments_per_section + k],
                                 None,
                                 None)
                mesh.addMeshPart(0, [4 * num_segments_per_section + k + 1, 4 * num_segments_per_section + k,
                                     5 * num_segments_per_section + k, 5 * num_segments_per_section + k + 1],
                                 None,
                                 None)
                for j in range(num_segments_on_arc - 1):
                    mesh.addMeshPart(0, [k + num_segments_per_section * (j + 6),
                                         k + num_segments_per_section * (j + 6) + 1,
                                         k + num_segments_per_section * (j + 7) + 1,
                                         k + num_segments_per_section * (j + 7)],
                                     None,
                                     None)


            #     mesh.addMeshPart(0, [2 * num_segments_per_section + k + 1, 2 * num_segments_per_section + k,
            #                          3 * num_segments_per_section + k, 3 * num_segments_per_section + k + 1],
            #                      None,
            #                      None)
            else:
                mesh.addMeshPart(0, [0, num_segments_per_section - 1,
                                     2 * num_segments_per_section - 1, num_segments_per_section],
                                 None, None)
                mesh.addMeshPart(0, [3 * num_segments_per_section - 1, 2 * num_segments_per_section,
                                     3 * num_segments_per_section, 4 * num_segments_per_section - 1],
                                 None, None)
                mesh.addMeshPart(0, [4 * num_segments_per_section, 5 * num_segments_per_section - 1,
                                     6 * num_segments_per_section - 1, 5 * num_segments_per_section],
                                 None, None)
                for j in range(num_segments_on_arc - 1):
                    mesh.addMeshPart(0, [num_segments_per_section * (j + 6) + k,
                                         num_segments_per_section * (j + 6),
                                         num_segments_per_section * (j + 7), num_segments_per_section * (j + 7) + k],
                                     None,
                                     None)
        #     mesh.addMeshPart(0, [num_segments_per_section, 2 * num_segments_per_section - 1,
        #                          4 * num_segments_per_section + num_segments_per_section - 1,
        #                          4 * num_segments_per_section], None, None)
        #     mesh.addMeshPart(0, [2 * num_segments_per_section, 3 * num_segments_per_section - 1,
        #                          3 * num_segments_per_section - 1,
        #                          2 * num_segments_per_section], None, None)
        mesh.addMeshPart(0, tmp_index_array_start, None, None)
        mesh.addMeshPart(0, tmp_index_array_end, None, None)
        mesh.addMeshPart(0, tmp_index_array_outside, None, None)
        mesh.addMeshPart(0, tmp_index_array_center, None, None)

        # mesh.addMeshPart(0, tmp_index_array_outside, None, None)
        # mesh.addMeshPart(0, tmp_index_array_center, None, None)
        # mesh.addMeshPart(0, tmp_index_array_se, None, None)
        # for i in range(num_segments_per_section):
        #     if
        # mesh.addMeshPart(0, [0, 3, 2, 1], None, None)
        # mesh.addMeshPart(0, [3, 7, 6, 2], None, None)
        # mesh.addMeshPart(0, [1, 2, 6, 5], None, None)
        # mesh.addMeshPart(0, [0, 4, 7, 3], None, None)
        # mesh.addMeshPart(0, [0, 1, 5, 4], None, None)
        # mesh.addMeshPart(0, [4, 5, 6, 7], None, None)
        # mesh.addMeshPart(0,[(1,0,0),(1,1,0),(1,1,1),(1,0,1)],None,None)
        # mesh.addMeshPart(0,[(1,1,0),(0,1,0),(0,1,1),(1,1,1)],None,None)
        # mesh.addMeshPart(0,[(0,0,0),(0,0,1),(0,1,1),(0,1,0)],None,None)
        # mesh.addMeshPart(0,[(0,0,0),(1,0,0),(1,0,1),(0,0,1)],None,None)
        # mesh.addMeshPart(0,[(0,0,1),(1,0,1),(1,1,1),(0,1,1)],None,None)

        newFeature.setGeometry(mesh)
        self.pyoutput(newFeature)

    def close(self):
        """This method is called at the end of the class."""
        pass
